<template>
    <view class="supps-info" :style="{marginLeft: isDrawerOpen ? '0' : '345px'}">
        <view class="info-content">
            <view class="b-info">
                <SupplierInfoImg
                    :avatar="avatar"
                    :name="name"
                    :cellphone="cellphone"
                    :company="company"
                    :isShowCp="isShowCp"
                    :isShowName="isShowName"
                    @showCustmInfo="showCustmInfo"
                >
                </SupplierInfoImg>
                <SupplierInfoBox
                    :cellphone="cellphone"
                    :district="district"
                    :country="country"
                    :address="address"
                    :amount="amount"
                    :rece="rece"
                    :price_count="price_count"
                    :money="money"
                    @countSaleMoney="countSaleMoney"
                    @payMoneyRece="paymentFlowMoney"
                    @receMoney="receMoney"
                >
                </SupplierInfoBox>
                <view class="c-list-box">
                    <SupplierInfoTxt ref="SupplierInfoTxt" :isDrawerOpen="isDrawerOpen"></SupplierInfoTxt>
                </view>
                <SupplierFixedBottom
                    v-if="!isDrawerOpen"
                    @optionAct="optionAct"
                    :isOld="isOld"
                ></SupplierFixedBottom>
            </view>
            <view class="c-info" v-show="showInfo">
                <SupplierInfo
                    :isShowCp="isShowCp"
                    :isShowName="isShowName"
                    :avatar="avatar"
                    :name="name"
                    :company="company"
                    :cellphone="cellphone"
                    :email="email"
                    :typeid_name="typeid_name"
                    :price_i_name="price_i_name"
                    :ydListTitle="ydListTitle"
                    :uid_cp="uid_cp"
                    :remark="remark"
                    :from_name="from_name"
                    @closeSuppinfo="closeSuppinfo"
                >
                </SupplierInfo>
            </view>
        </view>
        <popFlowWin
            ref="popFlowWin"
            @openReceMoneyWin="openReceMoneyWin"
        ></popFlowWin>
        <!-- 收款，退款窗口 -->
        <payMent ref="payMent"></payMent>
        <!-- 二维码支付 -->
        <payQrcode ref="payQrcode"></payQrcode>
        <popadvanceWin
            ref="popadvanceWin"
            @loadsuppinfoFun="loadsuppinfoFun"
        ></popadvanceWin>
    </view>
</template>

<script>
import ordertoptitle from "../bills/OrderTopTitle.vue";
import popFlowWin from "../comm/PopFlowWin.vue";
import customCtrl from "../../js/custom/CustomCtrl.js";
import https from "../../js/https/Https.js";
import lang from "../../js/conf/Lang.js";
import cmd from "../../js/Cmd.js";
import staticConst from "../../js/const/StaticConst.js";
import payMent from "../bills/PaymentMask.vue";
import config from "../../js/conf/Config.js";
import payQrcode from "../comm/PayQrcode.vue";
import popadvanceWin from "../comm/popadvanceWin.vue";

import SupplierInfoImg from "./SupplierInfoImg.vue";
import SupplierInfoBox from "./SupplierInfoBox.vue";
import SupplierInfoTxt from "./SupplierInfoTxt.vue";
import SupplierFixedBottom from "./SupplierFixedBottom.vue";
import SupplierInfo from "./SupplierInfo.vue";
let pageSize = staticConst.PAGE_SIZE;

export default {
    components: {
        ordertoptitle,
        SupplierInfoImg,
        SupplierInfoBox,
        SupplierInfoTxt,
        SupplierFixedBottom,
        SupplierInfo,
        popFlowWin,
        payMent,
        payQrcode,
        popadvanceWin,
    },
    props: {
        suppid: {
            type: String,
            default: ''
        },
        isDrawerOpen: {
            type: Boolean,
            default: false
        }
    },
    watch: {
        suppid: {
            handler(newVal) {
                if(newVal){
                    this.loadsuppinfo({id: newVal});
                }
            },
            deep: true,
            immediate: true
        }
    },
    data() {
        return {
            title: "供应商信息",
            showInfo: false,
            suppinfo: null,
            dialogVisible: false,
            avatar: "", //供应商头像
            name: "", //供应商名称
            cellphone: "", //供应商手机
            company: "", //供应商公司名称

            address: "", //地址
            district: "", //省市区
            country: "", //国家
            amount: "", //累计销售
            rece: "", //初始欠款
            price_count: "", //订单应收
            money: "", //预付款
            isShowCp: true, //是否显示供应商公司名称
            isShowName: true, //是否显示供应商名称

            email: "", //邮箱地址
            typeid_name: "", //供应商类别
            price_i_name: "", //默认报价
            uid_cp: "", //经手人吗
            ydListTitle: "", //开放云店
            ydList: [], //云店列表
            remark: "", //备注，
            remarkList: [], //备注列表
            from_name: "", //来源
            flowTitle: lang.supplierTxt.countSaleFlowTxt, //累计流水记录标题
            flowTitle: lang.supplierTxt.countSaleFlowTxt, //累计流水记录标题

            paymentFlowTxt: lang.supplierTxt.paymentFlowTxt, ////预付款流水标题
            pFlowPFrom: 0,
            payMentReceList: [], //预付款流水合并数据

            receTxt: lang.receTxt,
            receMoneyTxt: lang.supplierTxt.receMoneyTxt,
            rFlowPFrom: 0, //应收款列表
            customMoerInfo: null,

            isOld: 1, //是否是老供应商 1：是 ,其他不是

            allowPay: true,
            rules: {
                money: [
                    {
                        required: true,
                        message: "请输入预付金额",
                        trigger: "blur",
                    },
                    {
                        pattern: /^\d+(.\d{1,2})?$/,
                        message: "请输入正确的金额",
                        trigger: "blur",
                    },
                ],
                payid: [
                    {
                        required: true,
                        message: "请选择付款方式",
                        trigger: "change",
                    },
                ],
            },
        };
    },
    methods: {
        loadsuppinfoFun() {
            this.loadsuppinfo({ id: this.customMoerInfo.id });
        },
        /***关闭供应商详情**/
        showCustmInfo() {
            this.showInfo = !this.showInfo;
            cmd.external(cmd.CMD_SHOW_MORE, this.showInfo);
        },

        /****关闭供应商详情******/
        closeSuppinfo() {
            this.showInfo = false;
            cmd.external(cmd.CMD_SHOW_MORE, this.showInfo);
        },

        /****编辑供应商****/
        editsuppinfo(id) {
            this.loadsuppinfo(id, true);
        },

        /****获取供应商详情*****/
        loadsuppinfo(obj, isEdit = false) {
            this.clearData();
            let that = this;
            let param = {};
            if (!isEdit) {
                this.suppinfo = obj;
                param.id = obj.id;
            } else {
                param.id = obj;
            }
            param.is_yy = 0; //是否预约 1是 其他否 默认否
            https.getSuppmInfo(param, back);
            function back(data) {
                if (!data.data) {
                    that.clearData();
                    that.$refs.SupplierInfoTxt.initData(0);
                    return;
                }
                data = data.data;
                that.customMoerInfo = data;
                if (data.avatar) that.avatar = data.avatar;
                if (data.name) that.name = data.name;
                if (data.cellphone) that.cellphone = data.cellphone;
                if (data.company) that.company = data.company;
                if (data.address) that.address = data.address;
                if (data.district) that.district = data.district;
                if (data.country) that.country = data.country;
                if (data.amount)
                    //累计销售
                    that.amount = data.amount;
                if (data.rece)
                    //初始欠款
                    that.rece = data.rece;
                if (data.price_count)
                    //订单应收
                    that.price_count = data.price_count;
                if (data.money)
                    //预付款
                    that.money = data.money;

                if (data.email)
                    //邮箱地址
                    that.email = data.email;
                if (data.typeid_name)
                    //供应商类别
                    that.typeid_name = data.typeid_name;
                if (data.price_i_name)
                    //默认报价
                    that.price_i_name = data.price_i_name;
                if (data.uid_cp)
                    //经手人吗
                    that.uid_cp = data.uid_cp;
                that.isOld = data.isold; //是否是老供应商

                if (data.yd_ls) {
                    //开放云店
                    that.ydList = data.yd_ls;
                    let len = that.ydList.length;
                    for (let i = 0; i < len; i++) {
                        let obj = that.ydList[i];
                        that.ydListTitle +=
                            that.ydListTitle == "" ? obj.name : "," + obj.name;
                    }
                }
                if (data.remark)
                    //备注，
                    that.remark = data.remark;
                if (data.from_name)
                    //来源
                    that.from_name = data.from_name;

                that.$refs.SupplierInfoTxt.initData(data.id);

                if (isEdit) {
                    let cData = {};
                    cData.id = data.id;
                    cData.avatar = data.avatar;
                    if (data.tag_title)
                        cData.tag = String(data.tag_title).split(",");
                    else cData.tag = [];
                    cData.cellphone = data.cellphone;
                    cData.name = data.name;
                    cData.company = data.company;
                    cData.isold = data.isold;
                    cData.isold_name = data.isold_name;
                    that.$emit("updataCustomList", cData);
                }
            }

            this.isShowCp = customCtrl.getIsShowCp();
            this.isShowName = customCtrl.getIsShowName();
            this.$forceUpdate();
        },

        clearData() {
            this.suppinfo = null; //列表供应商信息
            this.customMoerInfo = null;
            this.avatar = ""; //客服头像
            this.name = ""; //客服名称
            this.cellphone = ""; //供应商手机
            this.company = ""; //供应商公司名称
            this.address = ""; //地址
            this.district = ""; //省市区
            this.amount = ""; //累计销售
            this.rece = ""; //初始欠款
            this.price_count = ""; //订单应收
            this.money = ""; //预付款
            this.email = ""; //邮箱地址
            this.typeid_name = ""; //供应商类别
            this.price_i_name = ""; //默认报价
            this.uid_cp = ""; //经手人吗
            this.ydListTitle = ""; //开放云店
            this.ydList = []; //云店列表
            this.remark = ""; //备注，
            this.remarkList = []; //备注列表
            this.from_name = ""; //来源

            this.pFlowPFrom = 0;
            this.rFlowPFrom = 0;
            this.isOld = 1;
            this.allowPay = true;
        },

        optionAct(act) {
            console.log("操作===act==:", act);
            if (act == 0)
                //删除供应商
                this.removeCustomTipWin();
            else if (act == 1)
                //开单
                this.bill();
            else if (act == 2) this.editSupplier();
            else if (act == 3) {
                const data = {
                    api: config.CG_SUPP_RECE_SAVE,
                    obj: this.customMoerInfo,
                    param: {
                        suppid: this.customMoerInfo.id,
                    },
                };
                this.$refs.popadvanceWin.initData(data);
            }
        },

        /****弹出删除供应商提示窗口****/
        removeCustomTipWin() {
            if (!this.suppinfo) return;
            let that = this;
            let data = {};
            data.title = lang.deleTitTxt;
            data.content = lang.supplierTxt.delTxt;
            data.showCancel = true;
            lang.showWin(data, back);
            function back(res) {
                if (res.confirm) {
                    that.closeSuppinfo();
                    that.remove();
                }
            }
        },

        /****删除供应商****/
        remove() {
            let that = this;
            let param = {};
            param.id = this.suppinfo.id;
            https.suppDel(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    that.$emit("removeCustom", that.suppinfo.id);
                }
            }
        },

        /********开单******/
        bill() {
            if (!this.suppinfo) return;
            let that = this;
            let data = {};
            data.title = lang.receiptTxt.titleTxt;
            data.content = lang.supplierTxt.billTxt;
            data.showCancel = true;
            lang.showWin(data, back);
            function back(res) {
                if (res.confirm) {
                    that.$emit("bill", that.suppinfo);
                }
            }
        },

        /***编辑供应商****/
        editSupplier() {
            if (!this.customMoerInfo) return;
            this.$emit("editSupplier", this.customMoerInfo);
        },

        /*******累计供货流水*******/
        countSaleMoney() {
            if (!this.suppinfo) return;
            let that = this;
            let param = {};
            param.page = 0;
            param.suppid = this.suppinfo.id;
            https.suppAmountList(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    if (!data.data) return;
                    data = data.data;
                    let obj = {};
                    obj.list = data;
                    obj.objType = 1;
                    obj.title = that.flowTitle;
                    obj.purchaseTxt = lang.purchaseTxt;
                    that.$refs.popFlowWin.show("show", obj);
                }
            }
        },

        /*********预付款流水************/
        paymentFlowMoney() {
            if (!this.suppinfo) return;
            this.pFlowPFrom = 0;
            let that = this;
            let param = {};
            param.suppid = this.suppinfo.id;
            param.pnum = pageSize;
            param.is_yy = 0;
            param.pfrom = this.pFlowPFrom * pageSize;
            https.suppReceList(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    // if(!data.data)return;
                    data = data.data;
                    if (!data) data = [];
                    let obj = {};
                    obj.list = magrePayMentReceList(data);
                    obj.objType = 2;
                    obj.title = that.paymentFlowTxt;
                    that.pFlowPFrom++;
                    that.$refs.popFlowWin.show("show", obj);
                }
            }

            function magrePayMentReceList(arr) {
                that.payMentReceList.map((v) => {
                    v.list = [];
                });
                let leng = arr.length;
                for (let i = 0; i < leng; i++) {
                    let temp = arr[i];
                    let hasIdx = -1;
                    let len = that.payMentReceList.length;

                    for (let j = 0; j < len; j++) {
                        let tObj = that.payMentReceList[j];
                        console.log("tObj======:", tObj);
                        if (tObj.atime == temp.atime) {
                            hasIdx = j;
                            break;
                        }
                    }
                    let t = {};
                    t.money = temp.money;
                    t.payinfo = temp.payinfo;
                    t.uid_cp = temp.uid_cp;
                    t.uid_name = temp.uid_name;
                    t.id = temp.id;
                    t.atime = temp.atime;
                    t.txt = temp.txt;
                    if (hasIdx != -1) {
                        //有相同日期
                        let tList = that.payMentReceList[hasIdx].list;

                        tList.push(t);
                        that.payMentReceList[hasIdx].list = tList;
                    } else {
                        //添加新的日期
                        let obj = {};
                        obj.atime = temp.atime;
                        obj.list = [];
                        obj.list.push(t);
                        that.payMentReceList.push(obj);
                    }
                }
                return that.payMentReceList;
            }
        },

        /*****应收订单列表******/
        receMoney() {
            if (!this.suppinfo) return;
            this.rFlowPFrom = 0;
            let that = this;
            let param = {};
            param.suppid = this.suppinfo.id;
            param.pnum = pageSize;
            param.is_yy = 0;
            param.pfrom = this.rFlowPFrom * pageSize;
            https.suppOderReceList(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    if (!data.data) return;
                    data = data.data;
                    let obj = {};
                    obj.objType = 3;
                    obj.title = that.receMoneyTxt;
                    obj.list = data.order_ls ? data.order_ls : [];
                    obj.recedata = data;
                    obj.recedata.type = "2";
                    obj.od_detail_all = data.od_detail_all;
                    that.rFlowPFrom++;
                    that.$refs.popFlowWin.show("show", obj);
                }
            }
        },

        async openReceMoneyWin(item) {
            if (!item) return;
            if (!this.allowPay) {
                return;
            }
            let that = this;
            let param = {};
            let url = config.CG_ORDER_PAY;

            param.type = 1; //this.orderInfos.type;
            param.odid = item.id || item.recdid;
            param.act = "topay";

            let res = await this.$api({ url: url, data: param });
            if (!res) return;
            if (res.status == 1) {
                if (!res.data) return;
                let obj = {};
                obj = res.data;
                obj.act = "pay_in";
                obj.odid = item.id || item.recdid;
                obj.type = 1;
                obj.typepay = "1";
                obj.callBack = topayAction;
                this.$refs.payMent.toggleBillsStateMask("show", obj);
            }

            async function topayAction(data) {
                if (data.payid >= 1000) {
                    //线下收款
                    let param = data;
                    let url = config.CG_ORDER_PAY;
                    let res = await that.$api({ url: url, data: param });
                    if (!res) return;
                    if (res.status == 1) {
                        that.$msg({ title: res.msg });
                        if (!res.data || !res.data.amount_notpay) return;
                        let obj = {};
                        obj.odid = data.odid;
                        obj.amount_notpay = res.data.amount_notpay;
                        that.$refs.popFlowWin.updateNotPayMoney(obj);
                    }
                } else {
                    let obj = {};
                    obj.payType = data.paytype;
                    obj.objtype = staticConst.PAY_OBJ_01; //data.objtype;
                    obj.odid = data.odid;
                    obj.money_pay = data.amount_receive;
                    that.$refs.payQrcode.creatQrcode(obj);
                }
            }
        },
    },
};
</script>
<style lang="scss" scoped>
.name-box {
    .supp-name {
        font-size: 16px;
    }
    .corporate-name {
        font-size: 12px;
    }
}


</style>
<style lang="scss">
.supps-info {
    flex: 1;
    margin-left: 345px;
    height: 100vh;
    background-color: #fff;
    .c-list-box {
        // overflow-y:auto;
        height: calc(100vh - 365px);
    }
    .info-content {
        display: flex;
    }
    .b-info {
        width: 100%;
        border-right: 1px solid #ececec;
        background: #fff;
    }

    .c-info {
        // width: 310px;
        // height: 100%;
    }
}
</style>